package com.oracle.applicaiton.controller;

import com.oracle.applicaiton.config.JWTConfig;
import com.oracle.applicaiton.service.LoginServiceApi;
import com.oracle.applicaiton.vo.MemberVo;
import com.oracle.application.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * @author bug制造者
 * @ClassName LoginController.java
 * @createTime 2021年07月08日 14:52:00
 * @Description
 */
@RestController
public class LoginController {

    @Autowired
    private LoginServiceApi loginServiceApi;

    @Autowired
    private JWTConfig jwtConfig;

    @GetMapping("/login")
    public R login(@RequestParam("loginname") String loginname,@RequestParam("pwd") String pwd, HttpServletResponse response){
        System.out.println(loginname+","+pwd);
        R<MemberVo> memberVoR = loginServiceApi.verifyLogin(loginname, pwd);
        if (memberVoR.getCode() == 200){
            MemberVo memberVo = memberVoR.getData();
            String token = jwtConfig.createToken(memberVo.getId(), memberVo.getLoginname());
            response.setHeader("token", token);
            return new R<>(200, "ok", null);
        }
        return memberVoR;
    }
}
